/*     */ package com.zimbra.cs.mailclient.imap;
/*     */ 
/*     */ import com.zimbra.cs.mailclient.util.Ascii;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class ImapData
/*     */ {
/*     */   public static enum Type
/*     */   {
/*  33 */     ATOM,  QUOTED,  LITERAL;
/*     */     
/*     */     private Type() {} }
/*  36 */   public static ImapData asAString(String s) { if (s.length() > 64) {
/*  37 */       return new Literal(Ascii.getBytes(s));
/*     */     }
/*  39 */     switch (getType(s)) {
/*     */     case ATOM: 
/*  41 */       return new Atom(s);
/*     */     case QUOTED: 
/*  43 */       return new Quoted(s);
/*     */     case LITERAL: 
/*  45 */       return new Literal(encodeUtf8(s));
/*     */     }
/*  47 */     return null;
/*     */   }
/*     */   
/*     */   public static ImapData asNString(String s) {
/*  51 */     return s != null ? asString(s) : Atom.NIL;
/*     */   }
/*     */   
/*     */   public static ImapData asString(String s) {
/*  55 */     return (s.length() <= 64) && (Chars.isText(s)) ? new Quoted(s) : new Literal(encodeUtf8(s));
/*     */   }
/*     */   
/*     */   private static Type getType(String s)
/*     */   {
/*  60 */     if (s.length() == 0) {
/*  61 */       return Type.QUOTED;
/*     */     }
/*  63 */     Type type = Type.ATOM;
/*  64 */     for (int i = 0; i < s.length(); i++) {
/*  65 */       char c = s.charAt(i);
/*  66 */       if (!Chars.isTextChar(c)) {
/*  67 */         return Type.LITERAL;
/*     */       }
/*  69 */       if (!Chars.isAStringChar(c)) {
/*  70 */         type = Type.QUOTED;
/*     */       }
/*     */     }
/*  73 */     return type;
/*     */   }
/*     */   
/*     */   private static byte[] encodeUtf8(String s) {
/*     */     try {
/*  78 */       return s.getBytes("UTF-8");
/*     */     } catch (UnsupportedEncodingException e) {
/*  80 */       throw new InternalError("UTF-8 charset not found");
/*     */     }
/*     */   }
/*     */   
/*     */   public static String asSequenceSet(List<? extends Number> ids)
/*     */   {
/*  86 */     StringBuilder sb = new StringBuilder();
/*  87 */     if (ids.isEmpty()) {
/*  88 */       return null;
/*     */     }
/*  90 */     Iterator<? extends Number> it = ids.iterator();
/*  91 */     sb.append(((Number)it.next()).longValue());
/*  92 */     while (it.hasNext()) {
/*  93 */       sb.append(',').append(((Number)it.next()).longValue());
/*     */     }
/*  95 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public abstract Type getType();
/*     */   
/*     */   public boolean isAtom() {
/* 101 */     return getType() == Type.ATOM;
/*     */   }
/*     */   
/*     */   public boolean isQuoted() {
/* 105 */     return getType() == Type.QUOTED;
/*     */   }
/*     */   
/*     */   public boolean isLiteral() {
/* 109 */     return getType() == Type.LITERAL;
/*     */   }
/*     */   
/*     */   public boolean isString() {
/* 113 */     return (isQuoted()) || (isLiteral());
/*     */   }
/*     */   
/*     */   public boolean isAString() {
/* 117 */     return (isAtom()) || (isString());
/*     */   }
/*     */   
/*     */   public boolean isNString() {
/* 121 */     return (isNil()) || (isString());
/*     */   }
/*     */   
/*     */   public boolean isNil() {
/* 125 */     return equals(Atom.NIL);
/*     */   }
/*     */   
/*     */   public abstract int getSize();
/*     */   
/*     */   public abstract byte[] getBytes() throws IOException;
/*     */   
/*     */   public InputStream getInputStream() throws IOException {
/* 133 */     return new SharedByteArrayInputStream(getBytes());
/*     */   }
/*     */   
/*     */   public void dispose() {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/imap/ImapData.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */